//
//  CarDetailBottomView.m
//  New_Runway
//
//  Created by mack on 2018/1/26.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CarDetailBottomView.h"
#import "CarSetAlertCell.h"
#import "CarOrderVC.h"
#import "SaleOutLibaryVC.h"
#import "SaleSetPriceVC.h"
#import "EditCarInfoVC.h"
#import "CarEspeciallybuyVC.h"

@interface CarDetailBottomView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIAlertViewDelegate>
@property (nonatomic, strong) NSMutableArray *controllers;

@end


@implementation CarDetailBottomView
{
    NSMutableArray *_nameArr;
    UIView *_bottomview;
    UICollectionView *_collectionview;
    NSInteger _height;
}
-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        _nameArr = [[NSMutableArray alloc] init];
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.1];
        
    }
    return self;
}

-(void)setInfomodel:(CarBaseInfoModel *)infomodel{
    
    _infomodel = infomodel;
    [self initSubview];

}
-(void)addAllViews{
    

    self.frame = CGRectMake(0, 0, Screen_width, Screen_height);
    UIView *bottomview = [[UIView alloc] init];
    bottomview.backgroundColor = kColorWhite();
    bottomview.frame = CGRectMake(0, self.frame.size.height, Screen_width, _height);
    [self addSubview:bottomview];
    _bottomview = bottomview;
    
    [self creatCollectionView];
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(27.5, _height - 80 , Screen_width - 55, 40)];
    cancelBtn.layer.masksToBounds = YES;
    cancelBtn.layer.cornerRadius = 5;
    cancelBtn.layer.borderColor = MAIN_NAV_COLOR().CGColor;
    cancelBtn.layer.borderWidth = 0.5;
    [cancelBtn setTitleColor:kColorWhite() forState:UIControlStateHighlighted];
//    [cancelBtn setBackgroundImage:[UIImage imageWithColor:MAIN_NAV_COLOR] forState:UIControlStateNormal];
    [cancelBtn setBackgroundImage:[UIImage imageWithColor:UIColorHex(0xffad80)] forState:UIControlStateHighlighted];
    [cancelBtn setTitle:@"取  消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:MAIN_NAV_COLOR() forState:UIControlStateNormal];
    [cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:17]];
    [bottomview addSubview:cancelBtn];
    [cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
}

-(void)initSubview{
    
    NSArray *name = @[@"车辆预定",@"销售定价",@"车辆特卖"];
    for (NSString *str in name) {
        [_nameArr addObject:str];
    }
    if (_infomodel.result.carPledgeState == 300 && _infomodel.result.carStockState == 20) {
        NSString *str = @"销售出库";
        [_nameArr addObject:str];
    }
    
    if (_infomodel.result.carStockState != 10) {
        //申请检测
        NSString *str = @"编辑车辆";
        [_nameArr addObject:str];
        
    }else{
        NSString *str = @"撤销入库";
        [_nameArr addObject:str];
    }
    if (_infomodel.result.carDetectState == 520) {
        //申请检测
        NSString *str = @"申请复检";
        [_nameArr addObject:str];

    }
    if (_infomodel.result.carStockState == 0 || _infomodel.result.carStockState == 1 || _infomodel.result.carStockState == 2) {
        //申请入库
        NSString *str = @"申请入库";
        [_nameArr addObject:str];

    }else if (_infomodel.result.carStockState == 20){

        if (_infomodel.result.carSaleState == 0 || _infomodel.result.carSaleState == 30) {
            //申请上架
            NSString *str = @"申请上架";
            [_nameArr addObject:str];

        }else if (_infomodel.result.carSaleState == 20 || _infomodel.result.carSaleState == 10){
            //申请下架
            NSString *str = @"申请下架";
            [_nameArr addObject:str];
        }
    }
    if ([NRWCarUser sharedUser].shopId) {
        if (_infomodel.result.carSiteState == 410) {
            //入场
            if (_infomodel.result.carPledgeState == 310) {
                //抵押
                NSString *str = @"申请临时出场";
                [_nameArr addObject:str];
            }
        }
    }
   

    if (_nameArr.count <= 6) {
        _height = 300;
    }else{
        _height = 400;
    }
    
    [self addAllViews];

   
}
-(void)viewAnimations:(NSInteger)height{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5];
        _bottomview.frame = CGRectMake(0, self.frame.size.height - height, Screen_width, height);
        
    } completion:^(BOOL finished) {
        
    }];
}

-(void)creatCollectionView{
    //创建一个layout布局类
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    //设置布局方向为垂直流布局
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //设置每个item的大小为100*100
//    layout.itemSize = CGSizeMake(100, 100);
    layout.minimumLineSpacing      = 0;
    layout.minimumInteritemSpacing = 0;
    //    [layout setHeaderReferenceSize:CGSizeMake(Screen_width, self.view.frame.size.width * 3 / 5)];
    //创建collectionView 通过一个布局策略layout来创建
   _collectionview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, Screen_width, _height) collectionViewLayout:layout];
    //代理设置
   _collectionview.backgroundColor = kColorWhite();
   _collectionview.delegate=self;
   _collectionview.dataSource=self;
    //注册item类型 这里使用系统的类型
    [_collectionview registerNib:[UINib nibWithNibName:@"CarSetAlertCell" bundle:nil] forCellWithReuseIdentifier:@"CarSetAlertCell"];
    [_bottomview addSubview:_collectionview];    
    _bottomview.frame = CGRectMake(0, self.frame.size.height, Screen_width, _height);
    [self viewAnimations:_height];


}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((Screen_width - 50) / 3, 100);
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _nameArr.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CarSetAlertCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"CarSetAlertCell" forIndexPath:indexPath];
    cell.iconImage.image = [UIImage imageNamed:_nameArr[indexPath.row]];
    cell.name.text = _nameArr[indexPath.row];
    
    return cell;
}


-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                       layout:(UICollectionViewLayout *)collectionViewLayout
       insetForSectionAtIndex:(NSInteger)section

{
  return UIEdgeInsetsMake(10, 10, 0, 10);//分别为上、左、下、右

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [self cancelClick];
//    kSelfWeak;
    [[CTMediator sharedInstance] Router_viewControllerForCarset:@{@"vc":_nameArr[indexPath.row],@"info":_infomodel,@"index":self.selIndex,@"reject":@(self.isReject)} callback:^(NSDictionary *info) {
        
        
    }];
    
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self cancelClick];
}
-(void)cancelClick{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0];
        _bottomview.frame = CGRectMake(0, self.frame.size.height, Screen_width, self.frame.size.height);
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];

    }];
}



@end
